<DIV id="mainContainer">

	<DIV id="leftCol">
	</DIV>
	
	<DIV id="contentCol">
	<h1>
		Search Results
	</h1>
	<DIV class="fbcontentdivider"></DIV>
<?php
	
require_once("Get_friends.php");
$F=new Friends();
if(isset($_GET['friend']))
{
	$F->Add_friends($_SESSION['info']['id_user'],$_GET['friend']);
}
if(isset($_GET['accept']))
{
	$F->Accept_friendship($_SESSION['info']['id_user'],$_GET['accept']);
}

require_once("db.class.php");
if(isset($_GET['request'])){$friendname=mysql_real_escape_string($_GET['request']); }

$bool_friend1="<a class = 'fbtab' href='main.php?page=searchresult&friend=";
$bool_friend2="'>ADD</a>";
$dbin = new DB();
$dbin2=new DB();	
$dbin->query("select lastname, firstname, id_user
				from user as u 
				where u.lastname like '%".$friendname."%' or u.firstname like '%".$friendname."%' Order by lastname;");
	echo "<DIV class='wrap'> 
				<DIV class='resultcontainer' style='font-weight: bold;'>
					<DIV class='lfloat'> Name </DIV><DIV class='rfloat'> Friendship </DIV>
				</DIV>
				<DIV class='fbcontentdivider'></DIV>";

$vide = 1;	
/////temp variable for reloading

while($dbin->fetch_assoc()){
	if($dbin->row['id_user']!=$_SESSION['info']['id_user']){	
		$bool_friend1="<a class = 'fbtab' href='main.php?page=searchresult&friend=";
		$id_friend=$dbin->row['id_user'];
		$bool_friend2="'>ADD</a>";
		$dbin2->query("select accept1 as a1, accept2 as a2 from friendship as u where u.id1=\"".$dbin->row['id_user']."\" and u.id2=\"".$_SESSION['info']['id_user']."\";");
		$request="&request=".$friendname;
		while($dbin2->fetch_assoc())
		{
			//echo $_SESSION['info']['id_user'];
			//if(isset($dbin2->row['a1'])) 
			//{
				
				
				if(($dbin2->row["a1"]==1) and ($dbin2->row["a2"]==1))
				{
					$bool_friend1="Already friends";
					$bool_friend2=" ";
					$id_friend="";
					$request="";
				}else if($dbin2->row["a1"]==0 and $dbin2->row["a2"]==1)
				{
					$bool_friend1="Friendship Requested";
					$bool_friend2="";
					$id_friend="";
					$request="";
				}else if($dbin2->row["a1"]==1 and $dbin2->row["a2"]==0)
				{
					$bool_friend1="<a class= 'fbtab' href='main.php?page=searchresult&accept=";
					$bool_friend2="'>Accept friendship</a>";

				}
		//}
		}
		echo "<DIV class='resultcontainer'>";
		if ($bool_friend1=="Already friends"){
			echo	"<DIV class='lfloat'><a href=main.php?page=profil&S=".$dbin->row['id_user'].">".$dbin->row['firstname']." ".$dbin->row['lastname']."</a></DIV>";
		}else{
			echo	"<DIV class='lfloat'>".$dbin->row['firstname']." ".$dbin->row['lastname']."</DIV>";
		}
		echo	"<DIV class='rfloat'>".$bool_friend1.$id_friend.$request.$bool_friend2."</DIV>";
		echo "</DIV>";
		$vide =0;
		echo "<DIV class='fbcontentdivider'></DIV>";
	}
	
}

	
	if($vide ==1) echo	"No result found !" ;
	echo "</DIV>";

?>

	</DIV>
</DIV>    
